package service.impl;

import dao.INySanYuanIncomeDao;
import dao.IPerformanceDao;
import model.NySanYuanFirsthandData;
import model.Performance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import service.NySanYuanIncomeService;
import util.UUIDGenerator;

import java.util.List;

/**
 * Created by lvdouStart on 2017/11/9.
 * 南方三院绩效设置service实现类
 */
@Service
public class NySanYuanIncomeServiceImpl implements NySanYuanIncomeService {

    @Autowired
    INySanYuanIncomeDao nySanYuanIncomeDao;

    @Autowired
    IPerformanceDao performanceDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setPerformance() {
        List<NySanYuanFirsthandData> list  = nySanYuanIncomeDao.queryFirsthandData();
        //分析每一条记录
        for(NySanYuanFirsthandData data : list){
            //每一条记录循环6次，每次分析两个月的
            for(int i=1;i<12;i+=2){
                Performance[] performances = getPerformanceList(data,i);
                performanceDao.insert(performances[0]);
                performanceDao.insert(performances[1]);
            }
        }
    }


    /**
     * 分析每两个月的原始数据
     * @param data  原始数据
     * @param month 月份
     * @return
     */
    public Performance[] getPerformanceList(NySanYuanFirsthandData data,int month){
        Performance[] performances  = new Performance[2];
        Performance performance = new Performance();
        performance.setId(UUIDGenerator.getUniqueLong());
        performance.setAssetsId(data.getAssetsId());
        performance.setAssetsName(data.getAssetsName());
        performance.setHospitalId(data.getHospitalId());
        performance.setTimeYear(data.getTimeYear());
        performance.setType(0);     //类型为收入
        double income = 0.0;        //每个月的收入(分)
        switch (month){
            case 1 :
                income = data.getMonth1()*100/2;
                break;
            case 3 :
                income = data.getMonth3()*100/2;
                break;
            case 5 :
                income = data.getMonth5()*100/2;
                break;
            case 7 :
                income = data.getMonth7()*100/2;
                break;
            case 9 :
                income = data.getMonth9()*100/2;
                break;
            case 11 :
                income = data.getMonth11()*100/2;
                break;

        }
        performance.setTimeMonth(month);
        performance.setTime(data.getTimeYear() + "-" + month + "-1");
        performance.setAmount(income);
        performances[0] = performance;
        Performance performance1 = (Performance)performance.clone();
        performance1.setId(UUIDGenerator.getUniqueLong()); //id重新生成
        performance1.setTimeMonth(month+1);
        performance1.setTime(data.getTimeYear()+ "-" + (month+1) + "-1");
        performances[1] = performance1;

        return performances;
    }
}
